package utils;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.net.InetAddress;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import exception.RuleBasedMonitoringException;
/**
 *
 * @author Alvinice
 */
public class Utils {
// ouvrir la connection avecle serveur de la machine distante
    public static JMXConnector remoteConnection(InetAddress hostIp,int hostPort) throws RuleBasedMonitoringException{
		JMXConnector jmxc=null;
                
                int port1 = 55777;      
		try {
			/*
			 * see: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html
			 */

			System.out.println("Connecting to node "+hostIp.getHostAddress());
		JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostIp.getHostAddress() + ":" + 
					port1  + "/jndi/rmi://" + hostIp.getHostAddress() + ":" + hostPort + "/jmxrmi");

			jmxc =JMXConnectorFactory.connect(url);
			 }
		catch (Exception e) {
			throw (new RuleBasedMonitoringException(e));
		}
		return jmxc;
	}
    //fermer la connexion avec le serveur de la machime sistante
    public static void closeremoteConnection(JMXConnector jmxc){
        try {
            jmxc.close();
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    System.out.println("Connection closes");
    }
    public static MBeanServerConnection mbeanServer(JMXConnector jmxc){
        MBeanServerConnection mbsc = MBeanServerFactory.newMBeanServer();
        try {
            mbsc = jmxc.getMBeanServerConnection();
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mbsc;
    }

    public static String[] mbeanDomains(MBeanServerConnection mbsc){
    String domains[]= new String[10];
        try {
            domains = mbsc.getDomains();
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    return domains;
    }

    public static Set mbeanNames(MBeanServerConnection mbsc){
    Set names = null;
        try {
            names = mbsc.queryNames(null, null);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    return names;
    }
    public static Set serverMbeans(MBeanServerConnection mbsc, ObjectName obj){
    Set mbeans = null;
        try {
            mbeans = mbsc.queryMBeans(obj, null);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
    return mbeans;
    }

    public static void printAttributes(ObjectName obj){

    }
    
}
